// components/record-button/index.js
import { language } from '../../utils/record'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    buttonDisabled: {
      type: Boolean,
      value: false,
      observer: function(newVal){
        let buttonType = newVal ? 'disabled' : 'normal'
        this.changeButtonType(buttonType)
      }
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    btnUrl: '../../assets/image/record/button_zh.png',
    buttonType: 'press',
    buttonConf: language[0],
    btnImg: {
      'normal': '../../assets/image/record/button_zh.png',
      'pess': '../../assets/image/record/button_zh_press.png',
      'disabled': '../../assets/image/record/button_zh_disabled.png'
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

    /**
     * 按下按钮开始录音
     */
    streamRecord(e) {
      if(this.data.buttonDisabled) {
        return
      }
      // 先清空背景音
      wx.stopBackgroundAudio()

      let currentButtonConf = this.data.buttonConf

      this.changeButtonType('press')

      this.triggerEvent('recordstart', {
        buttonItem: currentButtonConf
      })

    },

    /**
     * 松开按钮结束录音
     */
    endStreamRecord(e) {
      let currentButtonConf = this.data.buttonConf
      this.triggerEvent('recordend', {
        buttonItem: currentButtonConf
      })
    },

    /**
     * 修改按钮样式
     */
    changeButtonType(buttonType) {
      let temp = this.data.btnImg;
      this.setData({
        btnUrl: temp[buttonType]
      })
    },
  }
})